﻿
Imports System.Runtime.CompilerServices
Imports osi.root.connector
Imports osi.root.utils

Public Interface zipper
    Function zip(ByVal i() As Byte, ByRef o() As Byte) As Boolean
    Function unzip(ByVal i() As Byte, ByRef o() As Byte) As Boolean
End Interface

Public Module _zipper
    <Extension()> Public Function bypass(ByVal z As zipper) As Boolean
        Return assert(Not z Is Nothing) AndAlso
               object_compare(z, osi.service.zip.bypass.instance) = 0
    End Function

    <Extension()> Public Function bypass2(ByVal z As zipper) As Boolean
        Return assert(Not z Is Nothing) AndAlso
               object_compare(z, osi.service.zip.bypass2.instance) = 0
    End Function

    <Extension()> Public Function zip(ByVal z As zipper, ByVal i() As Byte) As Byte()
        Dim o() As Byte = Nothing
        assert(Not z Is Nothing)
        assert(z.zip(i, o))
        Return o
    End Function

    <Extension()> Public Function unzip(ByVal z As zipper, ByVal i() As Byte) As Byte()
        Dim o() As Byte = Nothing
        assert(Not z Is Nothing)
        assert(z.unzip(i, o))
        Return o
    End Function

    'accept empty string
    <Extension()> Public Function zip(ByVal z As zipper,
                                      ByVal i As String,
                                      ByRef o() As Byte) As Boolean
        If z Is Nothing Then
            Return False
        Else
            Dim ib() As Byte = Nothing
            ib = str_bytes(i)
            Return z.zip(ib, o)
        End If
    End Function

    <Extension()> Public Function unzip(ByVal z As zipper,
                                        ByVal i() As Byte,
                                        ByRef o As String) As Boolean
        If z Is Nothing Then
            Return False
        Else
            Dim ob() As Byte = Nothing
            If z.unzip(i, ob) Then
                o = bytes_str(ob)
                Return True
            Else
                Return False
            End If
        End If
    End Function
End Module
